package com.gpjcjl;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.base.lib.SharedHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;
import java.util.List;
import java.util.Stack;

/**
 * @author yinbiao
 * @date 2019/3/8
 */
public class App extends Application {

    private static final String TAG = "App";

    private static App instance;

    private String androidId;


    /**
     * 日志文件存储路径
     */
    public static final String LOG_PATH =
            Environment.getExternalStorageDirectory() + File.separator + BuildConfig.APPLICATION_ID + "/log/";


    /**
     * 缓存Activity栈
     */
    private Stack<Activity> stack = new Stack<>();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        int pid = android.os.Process.myPid();
        String processAppName = getAppName(pid);
        // 如果APP启用了远程的service，此application:onCreate会被调用2次
        // 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
        // 默认的APP会在以包名为默认的process name下运行，如果查到的process name不是APP的process name就立即返回
        if (processAppName == null || !processAppName.equalsIgnoreCase(getPackageName())) {
            Log.e(TAG, "enter the service process!");
            // 则此application::onCreate 是被service 调用的，直接返回
            return;
        }
        //初始化SharedPreference
        SharedHelper.getInstance().init(getApplicationContext());
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator(((context, layout) -> new ClassicsFooter(context)));

        CrashReport.initCrashReport(getApplicationContext(), "c7534706bd", true);

    }

    private String getAppName(int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> l = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : l) {
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
            }
        }
        return processName;
    }

    /**
     * 获取Application实例
     *
     * @return App
     */
    public static synchronized App getInstance() {
        return instance;
    }

    /**
     * Activity入栈
     *
     * @param context context
     */
    public void addContext(Activity context) {
        stack.push(context);
    }

    /**
     * Activity出栈
     *
     * @param context context
     */
    public void removeContext(Activity context) {
        stack.remove(context);
    }

    /**
     * 获取栈顶的Activity
     *
     * @return Activity
     */
    public Activity getTopContext() {
        return stack.peek();
    }


    public String getAndroidId() {
        if (!TextUtils.isEmpty(androidId)) {
            return androidId;
        } else if (!TextUtils.isEmpty(androidId = SharedHelper.getInstance().getString("androidId", null))) {
            return androidId;
        } else {
            androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
            SharedHelper.getInstance().put("androidId", androidId);
        }
        return androidId;
    }

}
